package com.example.groupservice.rabbitmq.consumer;

import com.example.groupservice.mapper.GroupInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.example.entity.constants.Constants;
import org.example.entity.po.GroupInfo;
import org.example.entity.query.GroupInfoQuery;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.support.Delivery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Description
 * @Author susu
 * @Date 2024/10/7
 */
@Component
@Slf4j
public class groupConsumer {
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private GroupInfoMapper<GroupInfo, GroupInfoQuery> groupInfoMapper;
    
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = Constants.GROUP_QUEUE),
            exchange = @Exchange(name = Constants.EXCHANGE_CHAT_TOPIC_NAME,type = ExchangeTypes.TOPIC),
            key = {Constants.GROUP_QUEUE_ROUTING_KEY}
    ))
    public void listenGroupQueue(Object msg,final Delivery delivery){
        String info = (String) msg;
        GroupInfo groupInfo = groupInfoMapper.selectByGroupId(info);
        CorrelationData correlationData = new CorrelationData(delivery.getProperties().getCorrelationId());
        rabbitTemplate.convertAndSend(delivery.getProperties().getReplyTo(),groupInfo,correlationData);
    }
}
